package online.foxnull2.foxmall.seckill.controller;

import online.foxnull2.common.utils.R;
import online.foxnull2.foxmall.seckill.service.SeckillService;
import online.foxnull2.foxmall.seckill.to.SeckillSkuRedisTo;
import online.foxnull2.foxmall.seckill.vo.SeckillSkuRedisVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SeckillController {

    private final SeckillService seckillService;

    @Autowired
    public SeckillController(SeckillService seckillService) {
        this.seckillService = seckillService;
    }

    @GetMapping("/currentSeckillSkus")
    public R getcurrentSeckillSkus() {
        List<SeckillSkuRedisTo> seckillSkuRedisTos = seckillService.getCurrentSeckillSkus();
        return R.ok().put("data", seckillSkuRedisTos);
    }

    @GetMapping("/{skuId}/seckillInfo")
    public SeckillSkuRedisVo getSeckillInfoBySkuId(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo seckillSkuRedisTo = seckillService.getSeckillInfoBySkuId(skuId);
        if (!ObjectUtils.isEmpty(seckillSkuRedisTo)) {
            SeckillSkuRedisVo seckillSkuRedisVo = new SeckillSkuRedisVo();
            BeanUtils.copyProperties(seckillSkuRedisTo, seckillSkuRedisVo);
            return seckillSkuRedisVo;
        }
        return null;
    }
}
